/*
 * Copyright 2011 Daniel Drown
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * getaddr.c - get a locally configured address
 */
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <netinet/in.h>
#include <strings.h>
#include <string.h>
#include <net/if.h>

#include "dhcpcd-files/netlink.h"
#include "dhcpcd-files/memory.h"
#include "getaddr.h"

// shared state between getinterface_ip and getaddr_cb
struct target {
  int family;
  int ifindex;
  union anyip ip;
  int foundip;
};

/* function: getaddr_cb
 * callback for getinterface_ip
 * nlm  - netlink message
 * data - info for which address we're looking for
 */
static int getaddr_cb(struct nlmsghdr *nlm, void *data) {
  struct ifaddrmsg *ifa_p;
  struct rtattr *rta_p;
  int rta_len;
  struct target *targ_p = (struct target *)data;
  
  if(nlm->nlmsg_type == NLMSG_DONE)
    return 1; // stop

  ifa_p = (struct ifaddrmsg *)NLMSG_DATA(nlm);
  rta_p = (struct rtattr *)IFA_RTA(ifa_p);

  if(ifa_p->ifa_index != targ_p->ifindex)
    return 0;

  if(ifa_p->ifa_scope != RT_SCOPE_UNIVERSE) 
    return 0;

  rta_len = RTM_PAYLOAD(nlm);
  for (; RTA_OK(rta_p, rta_len); rta_p = RTA_NEXT(rta_p, rta_len)) {
    switch(rta_p->rta_type) {
      case IFA_ADDRESS:
        if(targ_p->family == AF_INET6) {
          memcpy(&targ_p->ip.ip_ptr, RTA_DATA(rta_p), rta_p->rta_len - sizeof(struct rtattr));
          targ_p->foundip = 1;
          return 1;
        }
        break;
      case IFA_LOCAL:
        if(targ_p->family == AF_INET) {
          memcpy(&targ_p->ip.ip_ptr, RTA_DATA(rta_p), rta_p->rta_len - sizeof(struct rtattr));
          targ_p->foundip = 1;
          return 1;
        }
        break;
    }
  }

  return 0; // keep going
}

/* function: getinterface_ip
 * finds the first global IP of the given family for the given interface, or returns NULL.  caller frees pointer
 * interface - interface to look for
 * family    - family
 */
union anyip *getinterface_ip(const char *interface, int family) {
  struct {
    struct nlmsghdr nl;
    struct ifaddrmsg ifa;
  } req;
  struct target targ;
  union anyip *retval;

  targ.family = family;
  targ.foundip = 0;
  targ.ifindex = if_nametoindex(interface);
  if(targ.ifindex == 0) {
    return NULL; // interface not found
  }

  bzero(&req, sizeof(req));

  // set the NETLINK header
  req.nl.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
  req.nl.nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT;
  req.nl.nlmsg_type = RTM_GETADDR;

  // set the routing message header
  req.ifa.ifa_family = targ.family;

  send_netlink_cb(&req.nl, &getaddr_cb, &targ);

  if(targ.foundip) {
    retval = xmalloc(sizeof(union anyip));
    memcpy(retval, &targ.ip.ip_ptr, sizeof(union anyip));
    return retval;
  }

  return NULL;
}
